/**
 * 
 */
package com.mustafaiev.tair.autobase.jsptag;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.mustafaiev.tair.autobase.dao.WorkerDao;
import com.mustafaiev.tair.autobase.dao.WorkerDaoImpl;
import com.mustafaiev.tair.autobase.db.DBConnector;
import com.mustafaiev.tair.autobase.worker.Worker;

/**
 * @author tair
 * 
 */
public class DriversTag extends TagSupport {
    private static final Logger LOG = Logger.getLogger(OrdersTag.class.getName());
    private int driverId;
    
    public void setDriverId(final int driverId) {
        this.driverId = driverId;
    }

    public int getDriverId() {
        return driverId;
    }

    @Override
    public int doStartTag() throws JspException {

        DBConnector connector = DBConnector.getInstance();
        final WorkerDao dao = new WorkerDaoImpl();
        dao.setConnector(connector );
        
        List<Worker> worker = null;
        JspWriter w = pageContext.getOut();

        final StringBuffer buffer = new StringBuffer(1000);
        try {
            // table begin
            buffer.append("<table class=\"drivers\">");
            buffer.append("<thead>");
            buffer.append("<tr>");
            buffer.append("<th>Driver</th>");
            buffer.append("<th>Email</th>");
            buffer.append("</tr>");
            buffer.append("</thead>");

            worker = dao.getDrivers();
            Iterator<Worker> it = worker.iterator();
            while (it.hasNext()) {
                Worker driver = (Worker) it.next();

                buffer.append("<tr id=\"" + driver.getId()
                        + "\" class=\"driver\" >");
                buffer.append("<td class=\"d_name\">");
                buffer.append(driver.getName());
                buffer.append("</td>");
                buffer.append("<td class=\"d_email\">");
                buffer.append(driver.getEmail());
                buffer.append("</td>");

                buffer.append("</tr>");

            }

            // end of orders table
            buffer.append("</table>");

            // print table
            w.println(buffer.toString());

        } catch (Exception e) {
            throw new JspTagException("Drivers tag: " + e);
        }

        return SKIP_BODY;

    }

    @Override
    public int doEndTag() throws JspException {

        return EVAL_PAGE;
    }

}
